﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkDownFileSplitter.AIAnalaysis
{
    internal class OpenRouterUtils
    {
        public static string SummarizeContentOfLevel3(List<string> content)
        {
            string messsage = $"请根据标题{content[0]}，用20字以内总结出下面文字的内容：{string.Join("\n", content)}";
            return SendChatRequest(messsage);
        }

        public static string SendChatRequest(string userMessage)
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Authorization", $"Bearer {GlobalSetting.Instance.OpenRouterAPIKey}");

                var requestBody = new
                {
                    model = "deepseek/deepseek-chat-v3-0324:free",
                    messages = new[]
                    {
                        new { role = "user", content = userMessage }
                    }
                };

                var content = new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json");
                var response = client.PostAsync("https://openrouter.ai/api/v1/chat/completions", content).Result;

                response.EnsureSuccessStatusCode();
                var jsonResponse = response.Content.ReadAsStringAsync().Result;
                dynamic result = JsonConvert.DeserializeObject(jsonResponse);
                return result.choices[0].message.content;
            }
        }
    }
}
